/*
Copyright (c) 2008, Mike Gashler

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#include "GHtml.h"
#include "GHashTable.h"

GHtml::GHtml(const char* pDoc, int nSize)
{
	m_pDoc = pDoc;
	m_nSize = nSize;
	m_nPos = 0;
}

GHtml::~GHtml()
{
}

void GHtml::ParseTag()
{
	// Parse the tag name
	m_nPos++;
	while(m_pDoc[m_nPos] <= ' ' && m_nPos < m_nSize)
		m_nPos++;
	int nTagStart = m_nPos;
	while(m_pDoc[m_nPos] > ' ' && m_pDoc[m_nPos] != '>' && m_nPos < m_nSize)
		m_nPos++;
	int nTagNameLen = m_nPos - nTagStart;

	// Handle comment tags
	if(nTagNameLen >= 3 && strncmp(m_pDoc + nTagStart, "!--", 3) == 0)
	{
		// Find the end of the comment--todo: this isn't quite right
		while(m_nPos < m_nSize && m_pDoc[m_nPos] != '>')
			m_nPos++;

		// Handle the comment
		OnComment(&m_pDoc[nTagStart + 3], m_nPos - nTagStart - 5);

		// Advance past the '>'
		if(m_nPos < m_nSize)
			m_nPos++;

		// Eat whitespace
		while(m_nPos < m_nSize && m_pDoc[m_nPos] <= ' ')
			m_nPos++;
		return;
	}

	// Handle the tag
	OnTag(&m_pDoc[nTagStart], nTagNameLen);

	// Eat whitespace
	while(m_nPos < m_nSize && m_pDoc[m_nPos] <= ' ')
		m_nPos++;

	// Handle the params
	int nParamStart, nParamLen, nValueStart, nValueLen;
	while(m_pDoc[m_nPos] != '>' && m_nPos < m_nSize)
	{
		// Find the equals
		nParamStart = m_nPos;
		while(m_pDoc[m_nPos] != '=' && m_pDoc[m_nPos] != '>' && m_nPos < m_nSize)
			m_nPos++;
		nParamLen = m_nPos - nParamStart;
		m_nPos++;

		// Eat whitespace
		while(m_nPos < m_nSize && m_pDoc[m_nPos] <= ' ')
			m_nPos++;

		if(m_pDoc[m_nPos] == '"')
		{
			// Move past the '"'
			m_nPos++;
			nValueStart = m_nPos;

			// Find the close quote
			while(m_nPos < m_nSize && m_pDoc[m_nPos] != '"')
				m_nPos++;
			if(m_nPos >= m_nSize)
				break;
			nValueLen = m_nPos - nValueStart;
			m_nPos++;
		}
		else
		{
			// Move until we hit whitespace again
			nValueStart = m_nPos;
			while(m_nPos < m_nSize && m_pDoc[m_nPos] > ' ' && m_pDoc[m_nPos] != '>')
				m_nPos++;
			nValueLen = m_nPos - nValueStart;
		}

		// Handle the parameter
		OnTagParam(&m_pDoc[nTagStart], nTagNameLen, &m_pDoc[nParamStart], nParamLen, &m_pDoc[nValueStart], nValueLen);

		// Eat whitespace
		while(m_nPos < m_nSize && m_pDoc[m_nPos] <= ' ')
			m_nPos++;
	}

	// Advance past the '>'
	if(m_nPos < m_nSize)
		m_nPos++;

	// Eat whitespace
	while(m_nPos < m_nSize && m_pDoc[m_nPos] <= ' ')
		m_nPos++;
}

bool GHtml::ParseSomeMore()
{
	if(m_nPos >= m_nSize)
		return false;
	if(m_pDoc[m_nPos] == '<')
		ParseTag();
	else
	{
		int nChunkStart = m_nPos;
		m_nPos++;
		while(m_nPos < m_nSize && m_pDoc[m_nPos] != '<')
			m_nPos++;
		OnTextChunk(&m_pDoc[nChunkStart], m_nPos - nChunkStart);
	}
	return true;
}
